package com.example.service.impl;

import com.example.service.HystrixService;
import com.example.service.HystrixOpenFeignService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class HystrixServiceImpl implements HystrixService {
    @Autowired
    private HystrixOpenFeignService hystrixOpenFeignService;

    /**
     *coreSize
     * 线程池核心线程数，默认是10
     *
     * MAXIMUMSIZE
     * 设置线程池大小，默认是10
     *
     * maxQueueSize
     * 配置线程池任务队列的大小，默认值是-1，当使用-1时将使用SynchronousQueue作为队列，即意味这其实这个队列只是一个交换器，任务将直接交给工作线程处理。如果工作线程不足，哪任务将被拒绝，如果使用任何正整数，将使用LinkedBlockingQueue作为队列
     *
     * queueSizeRejectionThreshold
     * 表示等待队列超过阈值后开始决绝线程请求，默认是5，如果maxQueueSize为-1则该属性失效
     *
     * KeepAliveTimeMinutes
     * 设置活动保持时间，以分钟为单位，默认1分钟
     *
     * allowMaximumSizeToDivergeFromCoreSize
     * 配置maximumSize生效。这个值可以等于或高于coreSize,默认false
     */
    //限流操作
    @HystrixCommand(fallbackMethod = "limited_handler",threadPoolKey = "goods",threadPoolProperties = {@HystrixProperty(name="coreSize",value="1"),@HystrixProperty(name="maxQueueSize",value="1"),@HystrixProperty(name="queueSizeRejectionThreshold",value="1")})
    @Override
    public String getInfo() {
        return hystrixOpenFeignService.getOpenFeiInfo();
    }

    //强制熔断,适用于后台服务已经不在提供服务了
    //@HystrixCommand(fallbackMethod = "timeout_handler", commandProperties={@HystrixProperty(name="circuitBreaker.forceOpen",value="true")})

    @HystrixCommand(fallbackMethod = "timeout_handler", commandProperties = {@HystrixProperty(name="execution.isolation.threadInMilliseconds",value="3000")})
    @Override
    public String getTimeout() {
        return hystrixOpenFeignService.testTimeout();
    }


    public String timeout_handler(){
        return "hystrix return time out issue";
    }

    public String limited_handler(){
        return "server is busy,please waiting access!";
    }
}
